package com.gyms.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.okhttp.beans.HVDiscountProductBean;
import com.gyms.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import glide.ImageLoaderUtils;
import myInterface.OnItemClickLitener;
import utils.DecimalUtils;
import utils.StringUtils;

/**
 * 低价课程的适配器 yjl
 */
public class LowCourseAdapter extends RecyclerView.Adapter<LowCourseAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    // 场馆图片
    private List<HVDiscountProductBean> venuePic;
    private Context mContext;

    public LowCourseAdapter(Context context,List<HVDiscountProductBean> venuePic) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.venuePic = venuePic;
    }

    public void setData(List<HVDiscountProductBean> venuePic) {
        this.venuePic.clear();
        this.venuePic.addAll(venuePic);
        notifyDataSetChanged();
    }

    private OnItemClickLitener mOnItemClickLitener;//Item的点击监听

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_low_course, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
//        ImageLoaderUtils.display(mContext, holder.mIvGym, venuePic.get(position));
        //添加删除线
        holder.tvOldPrise.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        HVDiscountProductBean bean=venuePic.get(position);
        if(!StringUtils.isNull(bean)){
            initData(holder,bean);
        }
        //如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    private void initData(ViewHolder mViewHolder,HVDiscountProductBean mHVVenueBean) {
        mViewHolder.tvCorName.setText(""+mHVVenueBean.getProductName());
        mViewHolder.tvPrise.setText(DecimalUtils.setPriceStyle(mContext,mHVVenueBean.getDiscountPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
        BigDecimal nowPrise = new BigDecimal(mHVVenueBean.getDiscountPrice());
        BigDecimal oldPrise = new BigDecimal(mHVVenueBean.getOriginalPrice());
        if(nowPrise.compareTo(oldPrise)==0){
            mViewHolder.tvOldPrise.setVisibility(View.GONE);
        }else {
            mViewHolder.tvOldPrise.setText(DecimalUtils.setPriceStyle(mContext,mHVVenueBean.getOriginalPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
        }
        ImageLoaderUtils.display(mContext,mViewHolder.ivCorImg,mHVVenueBean.getProductUrl());
    }

    @Override
    public int getItemCount() {
        return null == venuePic ? 0 : venuePic.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cor_img)
        ImageView ivCorImg;
        @BindView(R.id.tv_cor_name)
        TextView tvCorName;
        @BindView(R.id.tv_prise)
        TextView tvPrise;
        @BindView(R.id.tv_old_prise)
        TextView tvOldPrise;

        ViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);
            ButterKnife.bind(this, view);
        }
    }

}
